# -*- coding: UTF-8 -*-

from ..action import Action


class BaseView(object):
    def __init__(self, action_id, env=None, type=None, fields=None):
        self.action = Action(action_id, env=env)
        self._type = type
        self.env = env
        self.fields = fields or {}

    @property
    def action_info(self):
        return self.action.info

    @property
    def res_model(self):
        return self.action_info['res_model']

    @property
    def Model(self):
        model = self.res_model
        Model = self.env.model(model, fields=self.fields)
        return Model

    @property
    def fields_list(self):
        return list(self.fields.keys())

    @property
    def view_info(self):
        return {
            'action': self.action_info,
            'view': {
                'fields': self.fields
            }
        }

    @property
    def context(self):
        return self.action.context

    def load_fields(self):
        model = self.res_model
        Model = self.env.model(model)
        action_info = self.action_info
        fields_raw = action_info['views'][self._type]['fields']
        fields_list = list(fields_raw.keys())
        info = Model.fields_get(fields_list)

        fields = {}
        for fld, meta in fields_raw.items():
            meta2 = info[fld]
            meta2.update(meta)
            fields[fld] = meta2

        self.fields = fields
        return fields
